/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

{%- set cases = [
    {
        'async_body': 'let order: string[] = [];
    order.push("before await");
    await Promise.resolve();
    order.push("after await");
    return order.join(", ");',
        'expected_result': "before await, after await"
    },
    {
        'async_body': 'try {
        await new Promise<void>((resolve, reject) => {
            reject(new Error("Error occurred"));
        });
    } catch (e: Error) {
        return "Caught: " + e.message;
    }
    return "Should not reach here";',
        'expected_result': "Caught: Error occurred"
    }
] -%}

{% from "16.concurrency/03.asynchronous_api/async_api_helper.jinja" import generate_async %}

{% macro generate_complete_test(declaration_type='function') %}
{% for c in cases %}

/*---
desc: Async {{declaration_type}}s are coroutines ({{declaration_type}}s which can be suspended and then resumed)
---*/

{% set params = {
    'name': 'resume',
    'body': c.async_body,
    'return_type': ': Promise<string>',
    'class_name': 'AsyncMethodReturnClass'
} %}

{{ generate_async(declaration_type, params) }}

function main() {
    const result = waitForCompletion({{ 'new AsyncMethodReturnClass().resume' if declaration_type=='method' else 'resume' }});
    arktest.assertEQ(result, "{{c.expected_result}}")
}

{% endfor %}
{% endmacro %}